package cn.com.shopec.erp.product.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.product.commo.CommodityCatConvertor;
import cn.com.shopec.erp.product.dao.ICommodityCatDao;
import cn.com.shopec.erp.product.entity.CatBrand;
import cn.com.shopec.erp.product.entity.CommodityCat;
import cn.com.shopec.erp.product.service.ICatBrandService;
import cn.com.shopec.erp.product.service.ICommodityCatService;
import cn.com.shopec.erp.product.service.ICommodityService;
import cn.com.shopec.erp.product.service.ISpecificationService;
import cn.com.shopec.erp.product.vo.CommodityCatVo;
import cn.com.shopec.erp.product.vo.CommodityVo;

/**
 * @ClassName: CommodityCatController
 * @Description: 商品分类controller
 * @author: gaoyaxiong
 * @date: 2013-5-30 下午14:14:13
 */
@Service
public class CommodityCatServiceImpl implements ICommodityCatService {
    private static Log log = LogFactory.getLog(CommodityCatServiceImpl.class);

    @Resource
    private ICommodityCatDao commodityCatDao;

    @Resource
    private CommodityCatConvertor catConvertor;

    @Resource
    private ICatBrandService catBrandService;

    @Resource
    private ICommodityService commodityService;
    
    @Resource
    private ISpecificationService specificationService;

    /**
     * @Title: getCommodityCatList
     * @Description: 获取分类列表
     * @param catVo
     * @return
     * @author: gaoyaxiong
     * @date: 2013-5-30下午01:31:46
     */
    @Override
    public List<CommodityCatVo> getCommodityCatList(CommodityCatVo catVo) {
        DetachedCriteria criteria = this.commodityCatDao.createCriteria();
        if (LogicUtil.isNotNull(catVo)) 
        {
            if (StringUtil.isExist(catVo.getCatPno())) 
            {
                criteria.add(Restrictions.eq("catPno", catVo.getCatPno()));
            }
            
            if (StringUtil.isExist(catVo.getCatNo())) {
                criteria.add(Restrictions.eq("catNo", catVo.getCatNo()));
            }
            
            if (StringUtil.isExist(catVo.getCatName())) {
                criteria.add(Restrictions.like("catName", "%" +catVo.getCatName()+ "%").ignoreCase());
            }
            
        }

        criteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));

        List<CommodityCat> catList = this.commodityCatDao.findByCriteria(criteria);
        return this.catConvertor.getCommodityCatVoList(catList);
    }
    
    /**
     * @Title: getCommodityCatList
     * @Description: 根据实体获取分类列表
     * @param catVo
     * @return
     * @author: gaoyaxiong
     * @date: 2013-5-30下午01:31:46
     */
    @Override
    public List<CommodityCat> getCommodityByCat(CommodityCatVo catVo) {
        DetachedCriteria criteria = this.commodityCatDao.createCriteria();
        if (LogicUtil.isNotNull(catVo)) 
        {
            if (StringUtil.isExist(catVo.getCatPno())) 
            {
                criteria.add(Restrictions.eq("catPno", catVo.getCatPno()));
            }
            
            if (StringUtil.isExist(catVo.getCatNo())) {
                criteria.add(Restrictions.eq("catNo", catVo.getCatNo()));
            }
            
            if (StringUtil.isExist(catVo.getCatName())) {
                criteria.add(Restrictions.eq("catName", catVo.getCatName()));
            }
            
        }

        criteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));

        List<CommodityCat> catList = this.commodityCatDao.findByCriteria(criteria);
        return catList;
    }

    /**
     * @Title: addCommodityCat
     * @Description: 添加分类
     * @param catVo
     * @param brandNO
     * @return
     * @author: gaoyaxiong
     * @throws Exception
     * @date: 2013-5-30下午19:31:46
     */
    @Override
    @Transactional
    public String addCommodityCat(CommodityCatVo catVo, String[] brandNO) {
        CommodityCat cat = new CommodityCat();
        cat = this.catConvertor.getCommodityCat(catVo);
        try {
        	this.commodityCatDao.save(cat);
        } catch (Exception e) {
            log.error("addCommodityCat " + e.getMessage(), e);
            return SZSCMConstant.FAIL;
        }
        // 添加分类品牌
//        if (brandNO != null) {
//            for (int i = 0; i < brandNO.length; i++) {
//                CatBrand cb = new CatBrand();
//                cb.setBrandNo(brandNO[i]);
//                cb.setCatNo(svCat.getCatNo());
//                String rs = this.catBrandService.AddCatBrand(cb);
//                if (rs == SZSCMConstant.FAIL || rs.equals(SZSCMConstant.FAIL)) {
//                    return SZSCMConstant.FAIL;
//                }
//            }
//        }

        return SZSCMConstant.SUCCESS;
    }

    @Override
    @Transactional
    public String updateCommodityCat(CommodityCatVo catVo, String[] brandNO) {
        if (LogicUtil.isNotNull(catVo)) {
            if (!StringUtil.isExist(catVo.getCatNo())) {
                return SZSCMConstant.FAIL;
            }
            CommodityCat cat = new CommodityCat();
            cat = this.commodityCatDao.findUniqueBy("catNo", catVo.getCatNo());
            if (null != cat) {
                cat.setCatPno(catVo.getCatPno());
                cat.setCatName(catVo.getCatName());
                cat.setNo(catVo.getNo());
                cat.setDescription(catVo.getDescription());
                try {
                	
                	  
//                    // 删除之前添加的品牌
//                    CatBrand catb = new CatBrand();
//                    catb.setCatNo(catVo.getCatNo());
//                    this.catBrandService.delectCatBrand(catb);
//                    // 添加分类品牌
//                    if (brandNO != null) {
//                        for (int i = 0; i < brandNO.length; i++) {
//                            CatBrand cb = new CatBrand();
//                            cb.setBrandNo(brandNO[i]);
//                            cb.setCatNo(cat.getCatNo());
//                            String rs = this.catBrandService.AddCatBrand(cb);
//                            if (rs == SZSCMConstant.FAIL || rs.equals(SZSCMConstant.FAIL)) {
//                                return SZSCMConstant.FAIL;
//                            }
//                        }
//                    }
                    // 修改分类
                    this.commodityCatDao.update(cat);

                } catch (Exception e) {
                    log.error("updateCommodityCat " + e.getMessage(), e);
                    return SZSCMConstant.FAIL;
                }
            } else {
                return SZSCMConstant.FAIL;
            }

        }
        return SZSCMConstant.SUCCESS;
    }

    @Override
    @Transactional
    public String delectCommodityCat(CommodityCatVo catVo) {
        if (LogicUtil.isNotNull(catVo)) {
            if (StringUtil.isExist(catVo.getCatNo())) {
                List<CommodityCatVo> listVo = this.getCommodityCatList(catVo);
                if (listVo.size() > 0) {
                	try {
	                    // 有子类不可删除
	                    CommodityCatVo Vo = new CommodityCatVo();
	                    Vo.setCatPno(catVo.getCatNo());
	                    if (this.getCommodityCatList(Vo).size() > 0) {
	                        return "该分类有子分类不可删除";
	                    }
	                    // 商品加入分类，分类不可删除
	                    CommodityVo commodityVo = new CommodityVo();
	                    commodityVo.setCatNo(catVo.getCatNo());
	                    if (this.commodityService.getCommodityList(commodityVo).size() > 0) {
	                        return "该分类被商品引用，不可删除";
	                    }
	                    //分类与品牌关联
	                    if(catBrandService.getBrand(catVo.getCatNo()).size() > 0){
	                    	return "该分类被品牌引用，不能删除";
	                    }
	                    //分类与规格关联
	                    if(specificationService.getSpecifications(catVo.getCatNo()) != null){
							return "该分类被规格引用，不能删除";
						}
	
	                    
	                    // 删除分类
	                    CommodityCat cat = this.catConvertor.getCommodityCat(listVo.get(0));
	                    cat.setDeleteFlag(SZSCMConstant.DELETE_FLAG_YES);

                        this.commodityCatDao.update(cat);
                        return SZSCMConstant.SUCCESS;
                    } catch (Exception e) {
                        log.error("delectCommodityCat " + e.getMessage(), e);
                        return SZSCMConstant.FAIL;
                    }
                }
            }
        }
        return "删除类别有误";
    }

    @Override
    public String getCommodityCatByNo(String no) {
        DetachedCriteria criteria = this.commodityCatDao.createCriteria();
        if (StringUtil.isExist(no)) {
            criteria.add(Restrictions.eq("no", no));
            criteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));

            List<CommodityCat> catList = this.commodityCatDao.findByCriteria(criteria);
            if (catList.size() > 0) {
                return SZSCMConstant.SUCCESS;
            } else {
                return SZSCMConstant.FAIL;
            }
        }
        return SZSCMConstant.FAIL;
    }

	@Override
	public List<CommodityCat> checkCatName(String catName) {
		DetachedCriteria detachedCriteria = commodityCatDao.createCriteria();
		
		if(null!=catName && LogicUtil.isNotNullAndEmpty(catName))
		{
			detachedCriteria.add(Restrictions.eq("catName", catName));
		}
		
		 detachedCriteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
		return commodityCatDao.findByCriteria(detachedCriteria);
	}

	/**
	 * 根据分类编号查询品牌名称
	 */
	@Override
	public List<CommodityCatVo> getCommodityCatNo(String catNo) {
		return commodityCatDao.getCommodityCatNo(catNo);
	}
}
